package hsh.processor;

import java.util.Stack;

import hsh.systemcalls.SystemCalls;
import hsh.systemcalls.SystemCalls.Call;
import hsh.util.Interruption;
import hsh.util.Status;
import hshs.process.Process;

public class Processor {


	private static Processor processor;
	private SystemCalls.Call registerTrap;
	private int sp;
	private Stack<String> paramenters;
	
	public Processor() {
		this.paramenters = new Stack<String>();
		
	}
	
	public SystemCalls.Call getRegisterTrap(){
		return this.registerTrap ;
	}
	
	public String nextParameter(){
		return this.paramenters.pop();
	}
	
	public void setRegisterTrap(SystemCalls.Call value){
		this.registerTrap = value;
	}
	
	public void storeStake(String parameter){
		this.paramenters.add(parameter);
	}
	
	public void setSP(int value){
		this.sp = value;
	}
	
	public Interruption executeProcess(Process process,int amountInstructions){
		
		
		
		
		for(int i=0;i<amountInstructions && !process.isEmpty() ;i++){
			
			
			System.out.print("PID " + process.getPid()+ " : ");
			Interruption interruption = process.executeNextInstruction();
			
		
			
			if(process.getStatus() == Status.FINISHED)
				break;
		
			if(interruption != Interruption.NONE)
				return interruption;
		}
		
		 return Interruption.NONE;
	}

	public static Processor getInstance() {
		if(processor == null){
			processor = new Processor();
		}
		
		return processor;
	}

	

}
